##########################################################################################

library(dplyr)
library(data.table)
library(optparse)
library(parallel)

##########################################################################################

option_list <- list(
    make_option(c("--maf_file"), type = "character") ,
    make_option(c("--images_path"), type = "character") ,
    make_option(c("--info_file"), type = "character")
)

if(1!=1){
    
    work_dir <- "~/20220915_gastric_multiple/dna_combine/"
    maf_file <- paste(work_dir,"/maf/All_GGA.all.maf",sep="")
	info_file <- paste(work_dir,"/config/tumor_normal.class.list",sep="")
    images_path <- paste(work_dir,"/images/mutRate",sep="")

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

maf_file <- opt$maf_file
info_file <- opt$info_file
images_path <- opt$images_path

dir.create(images_path , recursive = T)

###########################################################################################

info <- data.frame(fread(info_file))
dat_maf <- data.frame(fread( maf_file ))

###########################################################################################

Variant_Type <- c("Missense_Mutation","Nonsense_Mutation","Frame_Shift_Ins","Frame_Shift_Del","In_Frame_Ins","In_Frame_Del","Splice_Site","Nonstop_Mutation")

###########################################################################################

maf <- dat_maf[dat_maf$t_alt_count>0,]
maf_use <- data.frame(
    Hugo_Symbol = maf$Hugo_Symbol, 
    Chromosome = maf$Chromosome , Start_Position =  maf$Start_Position , End_Position = maf$End_Position ,
    Reference_Allele = maf$Reference_Allele , Tumor_Seq_Allele2 = maf$Tumor_Seq_Allele2 , 
    Variant_Classification = maf$Variant_Classification , Tumor = maf$Tumor_Sample_Barcode  )

maf_use <- subset( maf_use , Variant_Classification %in% Variant_Type )
maf_use <- merge( maf_use , info , by = "Tumor" )

###########################################################################################
## 位点
maf_use$vid <- paste(maf_use$Hugo_Symbol , 
    maf_use$Chromosome , maf_use$Start_Position , 
    maf_use$Reference_Allele , maf_use$Tumor_Seq_Allele2 , sep = ":")

#maf_use <- subset( maf_use , vid %in% dat_point$vid )

###########################################################################################
## 计算每个突变位点是否共享
result <- c()
for( normal in unique(maf_use$Normal) ){
    print(normal)
    tmp <- subset( maf_use , Normal == normal )
    tmp2 <- Reduce(function(x,y)rbind(x,y),mclapply(1:length(unique(tmp$vid)),function(i){
        mut <- unique(tmp$vid)[i]
        tmp2 <- subset( tmp , vid == mut )
        share <- length(grep( "IM" , unique(tmp2$Class))) > 0 & length(grep( "GC" , unique(tmp2$Class))) > 0
        if(share){
            tmp2$Share <- TRUE
        }else{
            tmp2$Share <- FALSE
        }
        tmp2
    },mc.cores=10)) 

    result <- rbind( result , tmp2 )
}

###########################################################################################

out_name <- paste0( images_path , "/MutShare.AllPoint.tsv" )
write.table( result , out_name , row.names = F , quote = F , sep = "\t" )